/*
 * @Author: LAPTOP-L879SH9C\GDJ 3131569316@qq.com
 * @Date: 2022-10-31 01:07:10
 * @LastEditors: LAPTOP-L879SH9C\GDJ 3131569316@qq.com
 * @LastEditTime: 2022-10-31 19:59:52
 * @FilePath: \springboot\src\main\java\com\exam\controller\QuestionnaireController.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package com.exam.controller;


import com.exam.entity.ApiResult;
import com.exam.entity.DataForm;
import com.exam.entity.Questionnaire;
import com.exam.serviceimpl.QuestionnaireServiceImpl;
import com.exam.util.ApiResultHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class QuestionnaireController {

    @Autowired
    private QuestionnaireServiceImpl questionnaireService;

    /**
     * 问卷列表
     * @param
     * @return
     */
    @PostMapping("/queryAll")
    public ApiResult queryAll( ){
        List<Questionnaire> questionnaireList = questionnaireService.queryAll();

//        for (int i = 0;i<questionnaireList.size();i++){
//            //将数据进行格式化，按照一定的格式，比如xxxx年xxxx月xxxx日
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            //将util.Date转换成为sql.Date，在Date.valueOf(String str)中发生了 util.Date.toSting(),就将Date类型转换成了String类型的字符串
//            Date answerTimeString= Date.valueOf(questionnaireList.get(i).getAnswerTime().toString());
//            //利用simpleDateFormat.format（）方法进行格式输出，他的作用就是按照第一步的格式进行输出时间
//            questionnaireList.get(i).setAnswerTimeString(simpleDateFormat.format(answerTimeString));
//        }


        int count = questionnaireList.size();
        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            ApiResult result =  ApiResultHandler.buildApiResult(200, "查询成功", questionnaireList);

            return result;
        }
    }
    /**
     * 回收站列表
     * @param
     * @return
     */
    @PostMapping("/queryRecycleAll")
    public ApiResult queryRecycleAll( ){
        List<Questionnaire> questionnaireList = questionnaireService.queryRecycleAll();
        int count = questionnaireList.size();

        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            ApiResult result =  ApiResultHandler.buildApiResult(200, "查询成功", questionnaireList);
            System.out.println(result.getData());
            return result;
        }
    }
    /**
     * 删除问卷，移入回收站
     * @param questionnaire
     * @return
     */
    @PostMapping("/deleteQuestionnaireById")
    public ApiResult quesestionnaireDeleteResult(@RequestBody Questionnaire questionnaire){
        int count = questionnaireService.deleteQuestionnaireById(questionnaire.getId()) ;
        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "删除失败！", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "成功移入回收站！", count);
        }
    }
    /**
     * 从回收站恢复问卷
     * @param questionnaire
     * @return
     */
    @PostMapping("/recycleQuestionnaireById")
    public ApiResult recycleQuestionnaireResult(@RequestBody Questionnaire questionnaire){
        int count = questionnaireService.recycleQuestionnaireById(questionnaire.getId()) ;
        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "恢复失败！", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "成功从回收站恢复！", count);
        }
    }

    /**
     * 新增问卷
     * @param
     * @return
     */
    @PostMapping("/saveQuestionnaire")
    public ApiResult saveQuestionnaire(@RequestBody DataForm dataForm){

        Questionnaire questionnaire = DataForm.toQuestionnaire(dataForm);
        int num = questionnaireService.saveQuestionnaire(questionnaire);
        if(num == 0){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "查询成功", null);
        }
    }

    /**
     * 从回收站彻底删除问卷
     * @param questionnaire
     * @return
     */
    @PostMapping("/finallyDeleteById")
    public ApiResult finallyDeleteResult(@RequestBody Questionnaire questionnaire){
        int count = questionnaireService.finallyDeleteById(questionnaire.getId()) ;
        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "彻底删除问卷失败！", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "成功彻底删除问卷！", count);
        }
    }
    /**
     * 模糊搜索问卷
     * @param
     * @return
     */
    @PostMapping("/fuzzySearchQuestionnaire")
    public ApiResult fuzzySearchQuestionnaireResult(@RequestBody Questionnaire questionName){

        List<Questionnaire> questionnaireList = questionnaireService.fuzzySearchQuestionnaire(questionName);
        int count = questionnaireList.size();


        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "查询成功", questionnaireList);
        }
    }
    /**
     * 发送问卷
     * @param questionnaire
     * @return
     */
    @PostMapping("/sendQuestionnaire")
    public ApiResult sendQuestionnaire(@RequestBody  Questionnaire questionnaire ){
        int count = questionnaireService.sendQuestionnaire(questionnaire.getId());
        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "查询成功", null);
        }
    }

    /**
     * 停止问卷
     * @param questionnaire
     * @return
     */
    @PostMapping("/stopQuestionnaire")
    public ApiResult stopQuestionnaire(@RequestBody  Questionnaire questionnaire ){
        int count = questionnaireService.stopQuestionnaire(questionnaire.getId());
        if(count == 0){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "查询成功", null);
        }
    }
    @PostMapping("/findQuestionnaireById")
    public ApiResult findQuestionnaireById(@RequestBody Questionnaire id){

        String question_id = id.getId();
        Questionnaire questionnaire = questionnaireService.findQuestionnaireById(question_id);
        if(questionnaire == null){
            return ApiResultHandler.buildApiResult(400, "查询失败", null);
        }else{
            return ApiResultHandler.buildApiResult(200, "查询成功", questionnaire);
        }
    }

}
